#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#

import os
from enum import Enum


AGENT_HOME = os.getenv("DATA_BACKUP_AGENT_HOME", None)


class AgentConstants:
    DB_PLUGIN_PATH = fr"{AGENT_HOME}/DataBackup/ProtectClient/Plugins/GeneralDBPlugin"
    LOG_PATH = fr"{AGENT_HOME}/DataBackup/ProtectClient/ProtectClient-E/slog/Plugins/GeneralDBPlugin"

    BIN_PATH = os.path.join(DB_PLUGIN_PATH, "bin")

    TMP_PATH = os.path.join(DB_PLUGIN_PATH, "tmp")
    STMP_PATH = os.path.join(DB_PLUGIN_PATH, "stmp")


class RPCToolConstants:
    RPC_TOOL_PATH = fr"{AgentConstants.BIN_PATH}/rpctool.sh"
    PARAM_FILE_PREFIX = "rpcInput_"
    RESULT_FILE_PREFIX = "rpcOutput_"


class SAPHANAConstants:
    BACKINT_PATH = fr"{AgentConstants.BIN_PATH}/backint"


class SAPHANADirectoryStructure:
    # 目录
    USR_SAP_SID_DIR = "/usr/sap/{SID}"
    HDB_DIR = os.path.join(USR_SAP_SID_DIR, "HDB{instance}")
    SYS_DIR = os.path.join(USR_SAP_SID_DIR, "SYS")

    # 文件
    GLOBAL_INI_FILE = os.path.join(SYS_DIR, "global", "hdb", "custom", "config", "global.ini")


class SAPHANAErrorCode(int, Enum):
    # 不支持选择当前认证模式
    NOT_SUPPORT_LOGIN_MODEL = 1577213495
    # 认证信息错误
    ERROR_AUTH = 1577209942
    # 注册租户数据库时，需要输入系统数据库用户名和密码
    LACK_SYSTEM_INFO = 1577213493
    # 登录系统数据库失败
    LOGIN_SYSTEM_DB_ERROR = 1577213494
    # 类型和所选主机不匹配
    TYPE_AND_AGENTS_NOT_MATCH = 1577213492
    # 自定义参数中的systemId在生产环境上不存在。
    SYSTEM_ID_NOT_EXIST = 1577213473
    # 数据库在生产环境上分布的节点（[{0}]）与资源注册时选的节点（[{1}]）不一致。
    DB_NODES_NOT_FULL_MATCH = 1577213474
    # 生产环境异常。
    DB_ENV_ERROR = 1577213475
    # 登录数据库失败。
    ACCESS_DB_ERROR = 1577213476
    # 不支持选择的部署类型。
    TENANT_MODEL_NOT_SUPPORT = 1577213490
    # 数据库在生产环境上不存在。
    DB_NOT_EXIST = 1577213477
    # 部署类型不匹配
    DEPLOY_TYPE_ERROR = 1577213484
    # 集群不在线
    CLUSTER_NOT_ONLINE = 1577213478
    # 参数错误
    ERROR_PARAM = 1677929218
    # 不允许更换保护的数据库
    NOT_ALLOW_CHANGE_PROTECT_DB = 1577213481
    # 不能修改数据库为新的数据库
    NOT_ALLOW_CHANGE_DB_TO_NEW_DB = 1577213482
    # 系统异常
    SYSTEM_ERROR = 1677929221
    # 数据库的服务（[{0}]）未运行。
    NOT_ALL_DB_SERVICE_RUNNING = 1577213479
    # 增量转全量
    INC_TO_FULL = 0x5E02502D
    # 生产环境上日志不连续。
    LOG_INCONSISTENT = 1577213480
    # 生产环境没有新的日志副本。
    NO_NEW_LOG_BACKUP = 1577213485
    # 生产环境上新生成的日志副本文件（[{0}]）不存在。
    LOG_BACKUP_FILE_NOT_EXIST = 1577213487
    # 生产环境上配置的日志备份目录（{0}）不存在。
    LOG_BACKUP_PATH_NOT_EXIST = 1577213486
    # 备份日志备份文件失败
    BACKUP_LOG_FILE_FAIL = 1577213488
    # 计算日志备份的开始和结束时间失败。
    CALCULATE_LOG_TIME_FAIL = 1577213489
    # 备份副本的数据库版本({0})与目标位置数据库版本({1})不匹配。
    VERSION_DISMATCH = 1577209921
    # 副本的拓扑结构与目标位置拓扑结构不匹配。
    TOPOLOGY_DISMATCH = 1577209926
    # 备份副本的数据库租户模式（{0}）与目标位置数据库租户模式（{1}）不一致
    MULTI_MODE_DISMATCH = 1577209927
    # 目标数据库未运行
    DB_NOT_RUNNING = 1577213496
    # 获取日志备份目录失败
    GET_LOG_BACKUP_PATH_FAIL = 1577213497
    # 数据库未关闭
    DB_NOT_CLOSE = 1577209950
    # system id不一致
    SYSTEM_ID_NOT_EQUAL = 1577213498
    # 执行备份/恢复命令失败
    EXEC_BACKUP_RECOVER_CMD_FAIL = 1577209989
    # 外置代理主机内存不足
    AGENT_INSUFFICIENT_MEMORY = 1677873256
    # 系统数据库与租户数据库不能互相恢复
    SYSTEM_TENANT_NOT_RECOVER_EACH_OTHER = 1577213499
    # 系统数据库恢复时，发现有正在运行的租户数据库恢复任务
    HANA_RESTORE_BUSY = 1577210038
    # 租户数据库恢复时，发现有正在运行的系统数据库恢复任务
    HANA_RESTORE_SYSTEMDB_BLOCK_TENANTDB = 1577213516
    # 修改数据库配置失败
    DB_CONFIG_FAILED = 1577213511
    # 获取版本号失败
    DB_VERSION_FAILED = 1577213512
    # 获取uuid是失败
    DB_UUID_FAILED = 1577213513
    # 路径不存在
    PATH_NOT_EXIST = 1577213514
    # 安装backint失败
    INSTALL_BACKINT_FAILED = 1577213515
    # 日志备份路径不在共享目录下。
    LOG_BACKUP_PATH_NOT_SHARED = 1577213590
    # 环境变量 {1} 未设置
    ENVIRONMENTS_NOT_SET = 1577213589


class SAPHANAJobLabel(str, Enum):
    # "子任务（{0}）备份成功，共备份数据量：{1}。"
    BACKUP_SUCCESS_LABEL = "hana_backup_success_label"
    # "数据保护代理主机（{0}）正在执行备份子任务（{1}），已备份数据量：（{2}）。"
    BACKUP_RUNNING_LABEL = "hana_backup_label"
    # "子任务（{0}）备份失败。"
    BACKUP_FAIL_LABEL = "plugin_backup_subjob_fail_label"
    # 执行前置任务失败
    PREREQUISITE_FAIL_LABEL = "plugin_execute_prerequisit_task_fail_label"
    # "生成子任务失败。"
    GENERATE_SUBJOB_FAIL_LABEL = "plugin_generate_subjob_fail_label"
    # 数据保护代理主机已经准备好恢复目录：（{0}），请用HANA studio完成恢复之后，再点击上报按钮，上报恢复结果
    RESTORE_SYSTEMDB_PATH_LABEL = "plugin_hana_systemdb_restore_label"
    # "子任务（{0}）恢复失败。"
    RESTORE_SUBJOB_FAIL_LABEL = "plugin_restore_subjob_fail_label"
    # 子任务（{0}）执行成功。
    RESTORE_SUBJOB_SUCCESS_LABEL = "plugin_task_subjob_success_label"
    # "开始执行恢复子任务（{0}）。"
    RESTORE_START_COPY_SUBJOB = "hana_start_restore_copy_subjob_label"
    # 开始执行恢复挂载子任务（{0}）。
    RESTORE_START_MOUNT_SUBJOB = "hana_plugin_start_restore_prepare_subjob_label"
    # 客户端 {1} 读取备份管道数据超时，建议停止备份任务。
    HANA_BACKUP_PIPE_TIMEOUT = "hana_backup_pipe_timeout"
    # 客户端 {1} 恢复任务超时，请参考数据备份特性指南(适用于 {2} )重新恢复。
    HANA_RESCOVERY_PROGRESS_TIMEOUT = "hana_recovery_progress_timeout"

